<?php

namespace app\tour\controller;

use think\Db;
use think\Request;
use app\tour\model\Ticket as TicketModel;
use think\Session;

class Ticket extends Common
{

    public function category(){

        if (Request::instance()->isAjax()){

            $data = TicketModel::getCategoryList();

            return ['code'=>0,'msg'=>'','data'=>$data];

        }else{

            return view();

        }

    }

    public function addCategory(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $addData = [
                'cname'=>$post['data']['cname'],
                'create_time'=>date("Y-m-d H:i:s"),
                'tour_id'=>Session::get('tour_id'),
                'tadmin_id'=>Session::get('t_admin_id'),
            ];

            $res = TicketModel::addCategory($addData);

            if ($res){

                return ['code'=>0,'msg'=>'添加成功'];

            }else{

                return ['code'=>1,'msg'=>'添加失败'];

            }

        }

    }

    public function editCategory(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData = [
                'cname'=>$post['data']['cname']
            ];

            $res = TicketModel::editCategory($post['data']['cid'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editCategoryIsUse(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData = [
                'is_use'=>$post['is_use']
            ];

            $res = TicketModel::editCategory($post['cid'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function delCategory(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData = [
                'is_del'=>1
            ];

            $res = TicketModel::editCategory($post['cid'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editCategorySort(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData = [
                'sort'=>$post['sort']
            ];

            $res = TicketModel::editCategory($post['cid'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }


        }

    }

    public function ticket(){

        if(Request::instance()->isAjax()){

            $data = TicketModel::getTicketList();

            return ['code'=>0,'msg'=>'','data'=>$data];

        }else{

            $category = TicketModel::getCategoryList();

            $this->assign('category',$category);

            return view();
        }

    }

    public function addTicket(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $addData = [

                'tname'             => $post['data']['tname'],
                'tour_id'           => Session::get('tour_id'),
                'price'             => $post['data']['price'],
                'is_discount'       => $post['data']['is_discount'] == 'on'?1:0,
                'discount_price'    => $post['data']['is_discount']?$post['data']['discount_price']:0,
                'discount_name_tag' => $post['data']['discount_name_tag'],
                'tadmin_id'         => Session::get('t_admin_id'),
                'create_time'       => date('Y-m-d H:i:s'),
                'start_time'        => $post['data']['start_time'],
                'end_time'        => $post['data']['end_time']
            ];

            $res = TicketModel::addTicket($addData);

            if ($res){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }

        }

    }

    public function editTicketIsUse(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $is_use = [
                'is_use'=>$post['is_use']
            ];

            $res = TicketModel::editTicket($post['tid'],$is_use);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editTicketCanRefund(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $can_refund = [
                'can_refund'=>$post['can_refund']
            ];

            $res = TicketModel::editTicket($post['tid'],$can_refund);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }


    public function editTicketIsDiscount(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $is_discount = [
                'is_discount'=>$post['is_discount']
            ];

            $res = TicketModel::editTicket($post['tid'],$is_discount);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }


    public function editTicketSort(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $sort = [
                'sort'=>$post['sort']
            ];

            $res = TicketModel::editTicket($post['tid'],$sort);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editTicket(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $updateData = [
                'tname'             => $post['data']['tname'],
                'price'             => $post['data']['price'],
                'discount_price'    => $post['data']['discount_price'],
               // 'can_refund'        => $post['data']['can_refund'],
                'discount_name_tag' => $post['data']['discount_name_tag'],
                'start_time' => $post['data']['start_time'],
                'end_time' => $post['data']['end_time']
            ];

            $res = TicketModel::editTicket($post['data']['tid'],$updateData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function delTicket()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('tour_ticket')->where('tid',$post['id'])->delete();
            if($res){
                 return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

}